package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.OSS;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.entity.Community;
import com.gxnzd.communityelderly.entity.DrugLibrary;
import com.gxnzd.communityelderly.entity.Employee;
import com.gxnzd.communityelderly.mapper.CommunityMapper;
import com.gxnzd.communityelderly.mapper.DrugLibraryMapper;
import com.gxnzd.communityelderly.mapper.EmployeeMapper;
import com.gxnzd.communityelderly.service.CommunityService;
import jakarta.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: CommunityServiceImpl
 * Package: com.gxnzd.communityelderly.service.impl
 * Description:
 *
 * @Author 胡海峰
 * @Create 2024/10/17 20:43
 * @Version 1.0
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    @Resource
    private CommunityMapper communityMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private DrugLibraryMapper drugLibraryMapper;

    @Resource
    private OSS oss;

    /**
     * 社区列表查询
     * @param page
     * @param pageSize
     * @param communityName
     * @param districtsName
     * @param communityAddress
     * @return
     */
    @Override
    public R<Page> getList(Integer page,
                           Integer pageSize,
                           String communityName,
                           String districtsName,
                           String communityAddress,
                           Long communityId
                           ) {
        Page<Community> pageInfo = new Page<>(page,pageSize);
        //查询条件
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(communityName),Community::getCommunityName,communityName)
                .eq(StringUtils.isNotEmpty(districtsName),Community::getDistrictsName,districtsName)
                .like(StringUtils.isNotEmpty(communityAddress),Community::getCommunityAddress,communityAddress);

        //对身份进行判断
        if(communityId != null){
            wrapper.eq(Community::getCommunityId,communityId);
        }

        communityMapper.selectPage(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    /**
     * 模糊查询
     * @param communityName
     * @return
     */
    @Override
    public R<List<Community>> queryKeyWords(String communityName) {
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(communityName),Community::getCommunityName,communityName);
        List<Community> communityList = communityMapper.selectList(wrapper);
        return R.success(communityList);
    }

    /**
     * 添加社区 （注意跟药品库的关联）
     * @param community
     * @return
     */
    @Override
    public R addCommunityInfo(Community community) {
        if(community.getCommunityImage().equals("")){
            return R.error("必须上传社区图片");
        }
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Community::getCommunityName,community.getCommunityName());
        Community selectOne = communityMapper.selectOne(wrapper);
        if(selectOne != null){
            return R.error("社区名称重复，添加失败！");
        }

        communityMapper.insert(community);
        return R.success("添加成功!");
    }

    /**
     * 修改社区
     * @param community
     * @return
     */
    @Override
    public R updateCommunityInfo(Community community) {
        if(community.getCommunityImage().equals("")){
            return R.error("必须上传社区图片");
        }
        LambdaQueryWrapper<Community> Cwrapper = new LambdaQueryWrapper<>();
        Cwrapper.eq(Community::getCommunityName,community.getCommunityName());
        Community selectOne = communityMapper.selectOne(Cwrapper);
        if(selectOne != null){
            return R.error("社区名称重复，修改失败！");
        }
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Community::getCommunityId,community.getCommunityId());
        Community one = communityMapper.selectOne(wrapper);
        if(one == null){
            return R.error("请传入正确的社区ID");
        }
        communityMapper.updateById(community);
        return R.success("修改成功");
    }

    /**
     * 删除社区（删除社区的时候要把药品库一并删除）
     * @param communityId
     * @return
     */
    @Override
    public R deleteCommunity(Long communityId) {
        LambdaQueryWrapper<Employee> empWrapper = new LambdaQueryWrapper<>();
        empWrapper.eq(Employee::getCommunityId,communityId);
        List<Employee> employeeList = employeeMapper.selectList(empWrapper);
        if(employeeList.size() != 0){
            return R.error("此社区还有人员，不能删除！");
        }
        //删除社区的时候也要将该社区的药品库一并删除
        LambdaQueryWrapper<DrugLibrary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrugLibrary::getCommunityId,communityId);
        drugLibraryMapper.delete(queryWrapper);
        //删除oss云端社区头像
        Community community = communityMapper.selectById(communityId);
        String[] urlList=community.getCommunityImage().split("/");
        oss.deletePhoto(urlList[urlList.length-1]);
        communityMapper.deleteById(communityId);
        return R.success("删除成功");
    }

    /**
     * 获取社区详情
     * @param communityId
     * @return
     */
    @Override
    public R<Community> getCommunityInfo(Long communityId) {
        return R.success(communityMapper.selectById(communityId));
    }

    /**
     * 获取所有社区列表
     * @return
     */
    @Override
    public R<List<Community>> getAllList() {
        LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
        List<Community> list = communityMapper.selectList(wrapper);
        return R.success(list);
    }
}
